﻿using Newtonsoft.Json.Linq;

namespace YAPACS.RisServer.Services.ECharts;

/// <summary>
/// 图表类
/// </summary>
public class EChart
{
    /// <summary>
    /// 图例
    /// </summary>
    public object Legend { get; set; } = new
    {
        Right = 20,
        Left = 20,
        Orient = "vertical"
    };


    /// <summary>
    /// 数据集
    /// </summary>
    public List<DataSource> DataSet { get; set; }


    /// <summary>
    /// 水平坐标轴
    /// （最多两个,否则会重叠）
    /// </summary>
    public List<Axis> XAxis { get; set; }


    /// <summary>
    /// 垂直坐标轴
    /// (最多两个，否则会重叠)
    /// </summary>
    public List<Axis> YAxis { get; set; }

    /// <summary>
    /// 系列集合
    /// </summary>
    public List<SeriesItem> Series { get; set; }


    /// <summary>
    /// 获取schema
    /// </summary>
    /// <returns></returns>
    public Dictionary<string, object> GetSchema()
    {
        Dictionary<string, object> schema = [];

        // 图例
        if (Legend != null)
        {
            schema.Add("legend", Legend);
        }

        // 数据集
        if (DataSet != null && DataSet.Count > 0)
        {
            if (DataSet.Count == 1)
            {
                schema.Add("dataset", DataSet[0].GetSchema());
            }
            else
            {
                JArray dataSet = new JArray();
                foreach (var item in DataSet)
                {
                    dataSet.Add(item.GetSchema());
                }
                schema.Add("dataset", dataSet);
            }
        }

        // 水平坐标轴
        if (XAxis != null && XAxis.Count > 0)
        {
            if (XAxis.Count == 1)
            {
                schema.Add("xAxis", XAxis[0].GetSchema());
            }
            else
            {
                JArray xAxis = new JArray();
                foreach (var item in XAxis)
                {
                    xAxis.Add(item.GetSchema());
                }
                schema.Add("xAxis", xAxis);
            }
        }


        // 垂直坐标轴
        if (YAxis != null && YAxis.Count > 0)
        {
            if (YAxis.Count == 1)
            {
                schema.Add("yAxis", YAxis[0].GetSchema());
            }
            else
            {
                JArray yAxis = new JArray();
                foreach (var item in YAxis)
                {
                    yAxis.Add(item.GetSchema());
                }
                schema.Add("yAxis", yAxis);
            }
        }

        // 系列集合
        if (Series != null && Series.Count > 0)
        {
            if (Series.Count == 1)
            {
                schema.Add("series", Series[0].GetSchema());
            }
            else
            {
                JArray series = new JArray();
                foreach (var item in Series)
                {
                    series.Add(item.GetSchema());
                }
                schema.Add("series", series);
            }
        }

        return schema;
    }
}
